package com.hmall.search.mq;

import com.hmall.common.MQConstants;
import com.hmall.search.service.impl.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * MQ消息监听器
 */
@Slf4j
@Component
public class ItemListener {
    @Resource
    private SearchService  searchService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.QUEUE_NAME_INSERT),
            exchange = @Exchange(name = MQConstants.EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key =MQConstants.ROUTING_KEY_INSERT
    ))
    public void listenItemUp(Long id){
        log.info("监听到上架的消息:{}",id);
        searchService.saveItemById(id);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.QUEUE_NAME_DELETE),
            exchange = @Exchange(name = MQConstants.EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = MQConstants.ROUTING_KEY_DELETE
    ))
    public void listenItemDown(Long id){
        log.info("监听到下架的消息:{}",id);
        searchService.deleteItemById(id);
    }
}
